<template>
  <div class="page-search">
    <lz-form v-bind="searchFormConfig" v-model:formValue="formData">
      <template #header>
        <h3 class="header">高级检索</h3>
      </template>
      <template #footer>
        <div class="footer">
          <el-button type="primary" @click="handleReset">
            <el-icon><Refresh /></el-icon>
            重置
          </el-button>
          <el-button type="primary" @click="handleSearch">
            <el-icon><Search /></el-icon>
            查询
          </el-button>
        </div>
      </template>
    </lz-form>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref,reactive } from 'vue'
import LzForm from '@/base-ui/form'
import { Refresh, Search } from '@element-plus/icons-vue'

export default defineComponent({
  props: {
    searchFormConfig: {
      type: Object,
      required: true
    }
  },
  components: {
    LzForm,
    Refresh,
    Search
  },
  emits: ['resetClick', 'searchClick'],
  setup(props, { emit }) {
    // 双向绑定的属性应该由配置文件的field来决定 formData的属性要动态决定
    const formItems: any = reactive(props.searchFormConfig.formItems)
    const formOriginData: any = {}
    for (const item of formItems) {
      formOriginData[item.field] = ''
    }
    const formData = ref(formOriginData)

    const handleReset = () => {
      // 双向绑定方案一
      // for (const key in formOriginData) {
      //   formData.value[key] = formOriginData[key]
      // }
      // 双向绑定方案二
      formData.value = formOriginData
      emit('resetClick')
    }

    const handleSearch = () => {
      emit('searchClick', formData.value)
    }

    return {
      formData,
      handleReset,
      handleSearch
    }
  }
})
</script>

<style scoped lang="less">
.footer {
  padding: 0 20px 10px 0;
  text-align: right;
}
</style>
